import QtQuick 1.1
import com.nokia.symbian 1.1
import com.nokia.extras 1.1

CommonDialog {
    id: root
    titleText: qsTr( "Select Day and Time" ) // dialog title

    privateCloseIcon: true        // show close button in the right corner of dialogs title
    buttonTexts: [ qsTr( "Ok" ) ] // Ok button

    property int   curDay;        // Qt.Monday = 1, Qt.Tuesday = 2, ... Qt.Sunday = 7
    property alias curHour:   colHour.selectedIndex // 0 - 23
    property alias curMinute: colMin.selectedIndex  // 0 - 59

    function launch() {
        fillModelsIfEmpty()
        colDay.selectedIndex = curDay - 1
        tumbler.privateInitialize()
        open()
    }

    function launchForNow() {
        var x = new Date()
        curMinute = x.getMinutes()
        curHour   = x.getHours()
        curDay    = dayIdxByLocalizedDayName( Qt.formatDateTime( x, "dddd" ) )
        launch()
    }

    onButtonClicked: accept() // ok was pressed

    content: Item {
        id: content
        height: screen.currentOrientation === Screen.Portrait ? 260 : 250  // in real project move this values to separate js-file 
        width:  screen.currentOrientation === Screen.Portrait ? 330 : 400  
        anchors.horizontalCenter: parent.horizontalCenter

        Tumbler {
            id: tumbler
            anchors.fill: parent
            anchors.margins: platformStyle.paddingSmall
            columns: [colDay, colHour, colMin]
            onChanged: root.curDay = colDay.selectedIndex + 1

            privateDelayInit: true

            TumblerColumn {
                id: colDay
                height: parent.height
                width: 190
                items: dayModel
                label: qsTr( "Day of Week" )
            }

            TumblerColumn {
                id: colHour
                height: parent.height

                items: hourModel
                label: qsTr( "Hrs" )
            }

            TumblerColumn {
                id: colMin
                height: parent.height

                items: minModel
                label: qsTr( "Mins" )
            }
        }
    }


    function localizedDayNameByIdx( idx )
    {
        if( idx >= Qt.Monday && idx <= Qt.Sunday )
        {
            var myDate = new Date()

            myDate.setFullYear( 1980, 2, 17 ) // March 17, 1980 - we know, it was monday
            myDate.setDate( myDate.getDate() + idx - 1 ) // Qt.Monday = 1

            return Qt.formatDateTime( myDate, "dddd" )
        }

        return ""
    }

    function dayIdxByLocalizedDayName( dayName )
    {
        var res = -1

        var myDate = new Date()
        myDate.setFullYear( 1980, 2, 17 ) // March 17, 1980 - we know, it was monday
        for( var i = 0; i < 7; i ++)
            if( Qt.formatDateTime( myDate, "dddd" ) == dayName )
            {
                res = i + 1;
                break
            }
            else
                myDate.setDate( myDate.getDate() + 1 )

        return res
    }



    // private
    function fillModelsIfEmpty()
    {
        if( !dayModel.count )
        {
            var myDate = new Date()
            myDate.setFullYear( 1980, 2, 17 ) // March 17, 1980 - we know, it was monday :)
            for( var i = 0; i < 7; i ++)
            {
                dayModel.append( { "value" : Qt.formatDateTime( myDate, "dddd" ) } )
                myDate.setDate( myDate.getDate() + 1 )
            }

            for( i = 0; i < 24; i++ )
                hourModel.append( { "value" : i >= 10 ? i : "0" + i } );

            for( i = 0; i < 60; i++ )
                minModel.append( { "value" : i >= 10 ? i : "0" + i } );
        }
    }

    ListModel { id: dayModel }
    ListModel { id: hourModel }
    ListModel { id: minModel }
}
